package com.tgjs.sphoto.adapter;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tgjs.sphoto.entity.PhotosDTO;
import com.tgjs.sphoto.activity.R;
import com.tgjs.sphoto.utils.GlideCircleTransform;

import java.util.List;
import java.util.Random;

/**
 * Created by dql on 2018/8/9.
 */

public class PhotoAdapter extends BaseQuickAdapter<PhotosDTO,BaseViewHolder> {

	public PhotoAdapter(@Nullable List<PhotosDTO> data) {
		super(R.layout.item_photos, data);
	}

	@Override
	protected void convert(final BaseViewHolder helper, PhotosDTO item) {
		//大图
		Glide.with(mContext).load(item.getUrls().getRegular()).asBitmap().listener(new RequestListener<String, Bitmap>() {
			@Override
			public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
				return false;
			}

			@Override
			public boolean onResourceReady(Bitmap bitmap, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
				 int width = bitmap.getWidth();
				 int height = bitmap.getHeight();
				FrameLayout.LayoutParams layoutParams  = new FrameLayout.LayoutParams(width,height);
				((ImageView) helper.getView(R.id.item_pic)).setLayoutParams(layoutParams);
				Random random = new Random();
				int r = random.nextInt(256);
				int g = random.nextInt(256);
				int b = random.nextInt(256);
				((ImageView) helper.getView(R.id.item_pic)).setBackgroundColor(Color.rgb(r,b,g));
				Log.d(TAG, "width:" + width);
				Log.d(TAG, "height:" + height);
				return false;
			}
		})
				.diskCacheStrategy(DiskCacheStrategy.NONE)
				.error(R.drawable.photo_error)
				.dontAnimate()
				.into((ImageView) helper.getView(R.id.item_pic));


/*		Glide.with(mContext).load(item.getUrls().getRegular())
				.asBitmap()
//				.thumbnail( 0.5f )//缩略图
//				.placeholder(R.drawable.pic_background)//图片加载出来前，显示的图片
				.diskCacheStrategy(DiskCacheStrategy.SOURCE)
				.error(R.drawable.photo_error)
				.dontAnimate()
				.into((ImageView) helper.getView(R.id.item_pic));*/
		//头像
		Glide.with(mContext)
				.load(item.getUser().getProfile_image().getMedium())
				.transform(new GlideCircleTransform(mContext))
				.into((ImageView) helper.getView(R.id.item_profileImage));
		helper.setText(R.id.item_userName,item.getUser().getName());
		helper.addOnClickListener(R.id.item_download);
		helper.addOnClickListener(R.id.item_profileImage);
	}
}
